//
//  HYLSnowflake.m
//  定时器雪花效果
//
//  Created by hyl on 16/6/26.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "HYLSnowflake.h"
static CGFloat snowY=0;
static int count=0;
@implementation HYLSnowflake
#pragma mark -drawRect
- (void)drawRect:(CGRect)rect {
    UIImage *image=[UIImage imageNamed:@"雪花"];
    [image drawAtPoint:CGPointMake(0, snowY)];
}
//-(void)awakeFromNib{
//    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
//    
//}
#pragma mark - awakeFromNib
-(void) awakeFromNib{
    //屏幕刷新时调用，屏幕一般一秒刷新60次
    CADisplayLink *link=[CADisplayLink displayLinkWithTarget:self selector:@selector(timeChange)];
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
-(void) timeChange{
    if (count==30) {
        snowY+=10;
        count=0;
    }else{
        count++;
    }
    
    if (snowY>self.bounds.size.height) {
        snowY=0;
    }
    //注意：这个方法并不会马上调用drawRect,其实这个方法只是给当前控件添加刷新的标记，等下一次屏幕刷新的时候才会调用drawRect
    [self setNeedsDisplay];
}
@end
